<template>
  <div class="app-container">
    <wise-crud ref="wise-crud" :urls="urls" :fields="fields" :handle="handle" :filters="filters">
      <template v-slot:handle="slotProps">
        <el-button type="info" size="small" class="activeBtn" round @click.native.prevent="downloadHanlde(slotProps.row)">下载</el-button>
      </template>
    </wise-crud>
  </div>
</template>

<script>
import WiseCrud from '@/components/Crud/wiseCrud'
import { downloadHandle } from '@/api'
export default {
  name: 'WarehouseManage',
  components: { WiseCrud },
  data() {
    return {
      handle: [],
      urls: {
        list: '/api/v1/welder-sbtx-second/project-info-comb/get-project-construction'
      },
      fields: [
        {
          label: '施工方案',
          prop: 'data.originalname',
          type: 'text',
          use: ['table']
        },
        { label: '操作', type: 'handle', use: ['table'], handle: [], tableWidth: 140 }
      ]
    }
  },
  computed: {
    spaceId() {
      return this.$store.getters.spaceId
    },
    filters() {
      return [
        {
          field: 'spaceId',
          operator: 'like',
          value: this.spaceId
        }
      ]
    }
  },
  methods: {
    downloadHanlde({ url }) {
      downloadHandle(process.env.VUE_APP_DOWNLOAD_URL, {
        obj: {
          url: process.env.VUE_APP_BASE_URL + process.env.VUE_APP_FILE_PATH + `/${url}`
        }
      }).then(res => {
        let disposition = res.headers['content-disposition'].replace(/\"/g, '').split('=')
        let fileName = decodeURI(disposition[disposition.length - 1])
        let blob = new Blob([res.data], { type: res.data.type })
        let downloadElement = document.createElement('a')
        let href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = fileName // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
